<?PHP

/*
 * 操作基础产品
 * 杨继康
 * 2012/07/22
 */

class Product_Model extends CI_Model {

    //重载CI_Model
    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('Base_Model');
    }

    /*
     * Insert_Product
     * 写入数据库
     */

    function Insert_Product($data = FALSE) {
        $data['pt_id'] = $this->Base_Model->GetID('pt_id', 'db_product');
        $result = $this->db->insert('db_product', $data);
        return $result;
    }

    /*
     * GetForState
     * 获取列表数据
     * 参数：状态
     */

    function GetListForState($str = FALSE) {
        $this->db->order_by("pt_id", "desc");
        $query = $this->db->get_where('db_product', array('pt_state' => $str));
        //返回结果
        return $query->result_array();
    }

    /*
     * GetEntity
     * 获取单挑数据
     * 参数：pt_id
     */

    function GetEntity($str = FALSE) {
        if ($str) {
            $query = $this->db->get_where('db_product', array('pt_id' => $str));
            //返回结果
            return $query->result_array();
        } else {
            $Array = array(array("pt_id" => '', "pt_name" => '', "pt_units" => ''));
            return $Array;
        }
    }

    /*
     * UpdateEntity
     * 更新单挑数据
     */

    function UpdateEntity($id = FALSE, $data = FALSE) {

        $this->db->where('pt_id', $id);
        $result = $this->db->update('db_product', $data);
        //返回结果
        return $result;
    }

    //删除数据
    function Dele($id = FALSE, $data = FALSE) {
        $this->db->trans_start(); //开启事务
        $this->db->where('pt_id', $id);
        $this->db->update('db_product', $data);
        $this->db->where('pt_id', $id);
        $this->db->update('db_series_product', array('sp_state' => OBJ_STATE_INVALID));
        $this->db->trans_complete();
    }

}

?>